//
//  JddSafeCenterViewController.m
//  PlayBall
//
//  Created by My Mac JiangDuoDuo on 14-7-29.
//
//

#import "JddSafeCenterViewController.h"
#import "JddViewControllerManager.h"
#import "JddwithdrawCashSecretViewController.h"
#import "JddRewithdrawCashSecretViewController.h"

@interface JddSafeCenterViewController ()

@end

@implementation JddSafeCenterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)loadNavBar
{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:@"安全中心"];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self loadNavBar];
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    self.listTable.tableFooterView = v;
    [self.listTable setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [self.listTable setSeparatorInset:UIEdgeInsetsZero];
    }
    tableHeadTitle = [NSArray arrayWithObjects:@"账户保护",@"资金保护", nil];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"SafeCenter"];
    [MobClick event:@"UserCenter-SafeCenter"];
    [self setJddTabBarHidden:YES];

    if([JddCommonFunction isLogin]){
        loginState = true;
    } else {
        loginState = false;
    }
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"SafeCenter"];
}
#pragma mark tableView的协议方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0://个人信息
                    [self personalInfoButtonClicked];
                    break;
                case 1://绑定手机
                    [self bindMobileButtonClicked];
                    break;
                case 2://修改密码
                    [self modifyPasswordButtonClicked];
                    break;
                default:
                    break;
            }
            
            break;
        case 1:
            switch (indexPath.row) {
                case 0://绑定银行卡
                    [self bindCardButtonClicked];
                    break;
                case 1://设置提款密码
                    [self withdrawCashSecretButtonClicked];
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    bgView.layer.borderColor = BORDER_COLOR;
    bgView.layer.borderWidth = 0.5;
    bgView.backgroundColor = [UIColor colorWithRed:250/255.0f green:250/255.0f blue:250/255.0f alpha:1.0];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 30)];
    label.backgroundColor = [UIColor clearColor];
    label.text = [tableHeadTitle objectAtIndex:section];
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor grayColor];
    [bgView addSubview:label];
    return bgView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30.0f;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 57.0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 3;
            break;
        case 1:
            return 2;
            break;
        default:
            return 0;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JddNewListViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"JddNewListViewCell" owner:self options:nil]lastObject];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.delegate = self;
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    cell.titleLabel.text = @"个人信息";
                    cell.desLab.text = @"领奖、提款的重要依据";
                    cell.imageIcon.image = [UIImage imageNamed:@"personal_massige.png"];
                    break;
                case 1:
                    cell.titleLabel.text = @"手机绑定";
                    cell.desLab.text = @"用户信息以及中奖提醒";
                    cell.imageIcon.image = [UIImage imageNamed:@"bindMobile.png"];
                    break;
                case 2:
                    cell.titleLabel.text = @"登录密码";
                    cell.desLab.text = @"建议定期更改";
                    cell.imageIcon.image = [UIImage imageNamed:@"xiugaiMiMa.png"];
                    break;
                default:
                    return nil;
                    break;
            }
            break;
        case 1:
            switch (indexPath.row) {
                case 0:
                    cell.titleLabel.text = @"银行卡";
                    cell.desLab.text = @"绑定银行卡，以便提款";
                    cell.imageIcon.image = [UIImage imageNamed:@"bindCard.png"];
                    break;
                case 1:
                    cell.titleLabel.text = @"提款密码";
                    cell.desLab.text = @"提款时验证，资金更安全";
                    cell.imageIcon.image = [UIImage imageNamed:@"tiKuanMiMa.png"];
                    break;
                default:
                    return nil;
                    break;
            }
            break;
        default:
            return cell;
            break;
    }
    cell.desLab.hidden = NO;
    cell.jianTou.hidden = NO;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
#pragma mark 某行cell被点击
//个人信息
- (void)personalInfoButtonClicked{
    if (loginState == false) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"您还没有登录，请先登录"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
        
    }else {
        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_PROFILE_UPDATE
                                                                fromController:self
                                                                    withObject:nil
                                                                   withObject2:nil];
        
    }
}
//绑定手机
- (void)bindMobileButtonClicked{
    if (loginState == false) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"您还没有登录，请先登录"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
        
    } else {
        if ([[JddUserProfile sharedInstance].IDCard length] < 6) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                            message:@"您的个人信息不完整，请先完善个人信息"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:@"取消",nil ];
            [alert show];
        } else {
            if ([[JddUserProfile sharedInstance].bindMobile length] > 0 && [[JddUserProfile sharedInstance].IsMobileBind intValue] == 1) {
                [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BIND_MOBILE_SUCCEEDED
                                                                        fromController:self
                                                                            withObject:nil
                                                                           withObject2:nil];
            } else {
                [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_RE_BIND_MOBILE
                                                                        fromController:self
                                                                            withObject:nil
                                                                           withObject2:nil];
            }
        }
        
        
    }
}
//绑定银行卡
- (void)bindCardButtonClicked{
//    if (loginState == false) {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
//                                                        message:@"您还没有登录，请先登录"
//                                                       delegate:self
//                                              cancelButtonTitle:@"确定"
//                                              otherButtonTitles:@"取消",nil ];
//        [alert show];
//    } else {
//        if ([[JddUserProfile sharedInstance].IDCard length]<6) {
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
//                                                            message:@"您的个人信息不完整，请先完善个人信息"
//                                                           delegate:self
//                                                  cancelButtonTitle:@"确定"
//                                                  otherButtonTitles:@"取消",nil ];
//            [alert show];
//        } else if (!([[JddUserProfile sharedInstance].bindMobile length]>0 && [[JddUserProfile sharedInstance].IsMobileBind intValue] == 1)){
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
//                                                            message:@"您的手机号码还未绑定，请先绑定手机号码"
//                                                           delegate:self
//                                                  cancelButtonTitle:@"确定"
//                                                  otherButtonTitles:@"取消",nil ];
//            [alert show];
//        } else {
            //if ([[JddUserProfile sharedInstance].BankCardNumber length] < 6) {
                [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BIND_CARD_CARD_INFO
                                                                        fromController:self
                                                                            withObject:nil
                                                                           withObject2:nil];
//            } else {
//                [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BIND_CARD_SUCCESSED
//                                                                        fromController:self
//                                                                            withObject:nil
//                                                                           withObject2:nil];
//            }
//        }
//    }
}
//修改密码
- (void)modifyPasswordButtonClicked{
    if (loginState==false) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"您还没有登录，请先登录"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
        
    } else {
        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_MODIFY_PASSWORD
                                                                fromController:self
                                                                    withObject:nil
                                                                   withObject2:nil];
        
    }
}
//设置提款密码
- (void)withdrawCashSecretButtonClicked{
    if (loginState==false) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"您还没有登录，请先登录"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
        
    } else {
        if ([[JddUserProfile sharedInstance].IsPayPwd intValue] == 1){
            JddRewithdrawCashSecretViewController *reWithdrawCash = [[JddRewithdrawCashSecretViewController alloc] init];
            [self.navigationController pushViewController:reWithdrawCash animated:YES];
        } else {
            JddwithdrawCashSecretViewController *withdrawCash = [[JddwithdrawCashSecretViewController alloc] init];
            [self.navigationController pushViewController:withdrawCash animated:YES];
        }
    }
}
#pragma mark alertView被点击
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView.message isEqualToString:@"您还没有绑定银行卡，请先绑定银行卡"]) {
        if (buttonIndex==0) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BIND_CARD_CARD_INFO
                                                                    fromController:self
                                                                        withObject:nil
                                                                       withObject2:nil];
        }
    }else if([alertView.message isEqualToString:@"您的个人信息不完整，请先完善个人信息"]){
        if (buttonIndex==0) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_PROFILE_UPDATE
                                                                    fromController:self
                                                                        withObject:nil
                                                                       withObject2:nil];
        }
    }
    else if([alertView.message isEqualToString:@"您的手机号码还未绑定，请先绑定手机号码"]){
        if (buttonIndex==0) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_RE_BIND_MOBILE
                                                                    fromController:self
                                                                        withObject:nil
                                                                       withObject2:nil];
        }
    }
    else{
        if (buttonIndex==0) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOGIN
                                                                    fromController:self
                                                                        withObject:nil
                                                                       withObject2:nil];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
